<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: blog.php 155 2013-09-22 07:52:43Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 155 $
* 	zuletzt geändert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-22 07:52:43 +0000 (Sun, 22 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

include ('includes/application_top.php');
require_once(DIR_FS_INC.'inc.date_short.php');

if($_GET['blog_cat'] == '') {
	$blog = $db->db_query("SELECT categories_id FROM ".TABLE_BLOG_ITEMS." WHERE item_id = '".(int)$_GET['blog_item']."' AND language_id = '".(int)$_SESSION['languages_id']."'");
	$_GET['blog_cat'] = $blog->fields['categories_id'];
}

$module_smarty = new Smarty();
$module_smarty->assign('tpl_path', 'templates/'.CURRENT_TEMPLATE.'/');
require(DIR_FS_CATALOG.'templates/'.CURRENT_TEMPLATE.'/source/boxes.php');

if(isset($_GET['blog_cat']) && is_numeric($_GET['blog_cat'])) {
	$getCatName = $db->db_query("SELECT
										titel
									FROM
										".TABLE_BLOG_CATEGORIES."
									WHERE
										categories_id = '".(int)$_GET['blog_cat']."'
									AND
										language_id = '".(int)$_SESSION['languages_id']."' ");

	$module_smarty->assign('cat_name', $getCatName->fields['titel']);
}

// Einzelner Beitrag
if(isset($_GET['blog_item'])) {

	$select_item = $db->db_query("SELECT
										name,
										title,
										description,
										allow_comment
									FROM
										".TABLE_BLOG_ITEMS."
									WHERE
										status = '2'
									AND
										item_id = '".(int)$_GET['blog_item']."'
									AND
										language_id = '".$_SESSION['languages_id']."'");
	
	if(BLOG_WROTE_COMMENTS == 'true' && $select_item->fields['allow_comment'] == '1') {
		if(BLOG_ONLY_REGISTER_USER == 'true') {
			if($_SESSION['customers_status']['customers_status_id'] !=1)
				include(DIR_WS_MODULES.'blog_comments.php');
		} else
			include(DIR_WS_MODULES.'blog_comments.php');
	}
	
	$module_smarty->assign('NAME', $select_item->fields['name']);
	$module_smarty->assign('TITEL', $select_item->fields['title']);
	$module_smarty->assign('TEXT', $select_item->fields['description']);
	
	$db->db_query("UPDATE 
						".TABLE_BLOG_ITEMS." 
					SET 
						shown = shown+1 
					WHERE 
						item_id = '".(int)$_GET['blog_item']."'
					AND
						language_id = '".$_SESSION['languages_id']."'");
	
// Wird Kategorie aufgerufen?
} elseif((isset($_GET['blog_cat']) && is_numeric($_GET['blog_cat'])) && (!isset($_GET['blog_item'])) && !isset($_GET['blog_year'])) {

	$getCatName = $db->db_query("SELECT
									titel
								FROM
									".TABLE_BLOG_CATEGORIES."
								WHERE
									categories_id = '".(int)$_GET['blog_cat']."'
								AND
									language_id = '".(int)$_SESSION['languages_id']."' ");

	$module_smarty->assign('cat_name', $getCatName->fields['titel']);
	$breadcrumb->add($getCatName->fields['titel'], href_link('blog.php', 'blog_cat='.(int)$_GET['blog_cat'], 'NONSSL'));

  	$items = array();
  	$blog_sql = "SELECT
						name, description, date, date_update, allow_comment, item_id, lenght
					FROM
						".TABLE_BLOG_ITEMS."
					WHERE
						status = '2'
					AND
						categories_id = '".(int)$_GET['blog_cat']."'
					AND
						language_id = '".(int)$_SESSION['languages_id']."'
					ORDER BY
						position ASC, date DESC";
	
	$listing = new page_break($blog_sql, $_GET['page'], 10, 'blog.php');
		
	$select_items = $listing->sql_string;
	
    while(!$select_items->EOF) { $cnt++;
		$desc = truncate($select_items->fields['description'], $select_items->fields['lenght']);
		
		$blog_monat_list = $monats_name[$monat_raw];
    	$items[$cnt] = array('name' => $select_items->fields['name'],
	    					'description' => strip_tags($desc),
	    					'date' => date_short($select_items->fields['date']),
	    					'date_update' => $select_items->fields['date_update'],
	    					'blog_link' => href_link(FILENAME_BLOG,'blog_cat='.(int)$_GET['blog_cat'].'&blog_item='.$select_items->fields['item_id']));
    	
    	if($select_items->fields['allow_comment'] == '1') {
    		$kommentare = $db->db_query("SELECT id FROM ".TABLE_BLOG_COMMENT." WHERE blog_id = '".$select_items->fields['item_id']."' ");
    		$items[$cnt]['kommentare'] = $kommentare->_numOfRows;
    	}
    	$select_items->MoveNext();
    }
	
	$module_smarty->assign('NAVIGATION', $listing->getLinks());
    $module_smarty->assign('blog_items', $items);
    $module_smarty->assign('BLOG', true);

} elseif(isset($_GET['y']) && isset($_GET['m'])) {
  	$items = array();
  	$select_items = $db->db_query("SELECT
										name,
										categories_id, 
										description, 
										date,
										item_id,
										lenght
									FROM
										".TABLE_BLOG_ITEMS."
									WHERE
										status = '2'
									AND
										date LIKE '".(int)$_GET['y']."-".str_pad((int)$_GET['m'], 2, '0', STR_PAD_LEFT)."%'
									AND
										language_id = '".(int)$_SESSION['languages_id']."'
									ORDER BY
										item_id DESC");
	
    while(!$select_items->EOF) {

    	$kommentare = $db->db_query("SELECT id FROM ".TABLE_BLOG_COMMENT." WHERE blog_id = '".$select_items->fields['item_id']."' ");

		$desc = truncate($select_items->fields['description'], $select_items->fields['lenght']);
		
    	$items[] = array('kommentare' => $kommentare->_numOfRows,
    					'name' => $select_items->fields['name'],
    					'description' => $desc,
    					'date' => date_short($select_items->fields['date']),
    					'blog_link' => href_link(FILENAME_BLOG,'blog_cat='.$select_items->fields['categories_id'].'&blog_item='.$select_items->fields['item_id'].'&y='.$_GET['y'].'&m='.$_GET['m']));
    	$select_items->MoveNext();
    }
	$module_smarty->assign('cat_name', 'Archiv für '.$monate[$_GET['m']].' '.$_GET['y']);
	$module_smarty->assign('blog_items', $items);
    $module_smarty->assign('BLOG', true);

} elseif(!isset($_GET['blog_cat']) && !isset($_GET['y'])) {

	$categories = $db->db_query("SELECT categories_id, titel FROM ".TABLE_BLOG_CATEGORIES." WHERE language_id = '".$_SESSION['languages_id']."' ORDER BY position ");

	$cats = array();
	while(!$categories->EOF) {
		$getItems = '';

		$getItems = $db->db_query("SELECT id FROM ".TABLE_BLOG_ITEMS." WHERE categories_id = '".$categories->fields['categories_id']."' AND language_id = '".$_SESSION['languages_id']."' ");

		$cats[] = array('num_of_items' => $getItems->_numOfRows,
    					'cat_title' => $categories->fields['titel'],
    					'cat_link' => href_link(FILENAME_BLOG, 'blog_cat='.$categories->fields['categories_id']));

		$categories->MoveNext();
	}
	$module_smarty->assign('cats', $cats);
	$module_smarty->assign('START',true);
}
require(DIR_WS_INCLUDES.'header.php');

$module_smarty->assign('language', $_SESSION['language']);
$main_content = $module_smarty->fetch(CURRENT_TEMPLATE.'/module/blog.html');
$smarty->assign('main_content', $main_content);

$smarty->assign('language', $_SESSION['language']);
if(!defined('RM'))
	$smarty->loadFilter('output', 'note');
$smarty->loadFilter('output','trimwhitespace');
$smarty->display(CURRENT_TEMPLATE.'/index.html');
include('includes/application_bottom.php');